package com.zb.common;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class SendSms {
	
    public static void sendSms(String phoneNumber,String code,String SMS) {
    	DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FjkXfJHVNqe9r3RKahD", "Y6At3I9AVrqjD1afUl6hydNaXFZKwM");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "����Ī��");
        request.putQueryParameter("TemplateCode", SMS);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
    
    public static final String getRandomSMSCode(int length, boolean numberCode) {
    	String randomSMSCode="";
        String codeTable = numberCode ? "1234567890" : "1234567890abcdefghijkmnpqrstuvwxyz";
        boolean flag = true;
        do {
            int count = 0;
            for (int i = 0; i < length; i++) {
                double dblR = Math.random() * codeTable.length();
                int intR = (int) Math.floor(dblR);
                char c = codeTable.charAt(intR);
                if (('0' <= c) && (c <= '9')) {
                    count++;
                }
                randomSMSCode += codeTable.charAt(intR);
            }
            if (count >= 2) {
            	flag = false;
            }
        } while (flag);
        return randomSMSCode;
    }
}
